package com.jdjc.auth.domin.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jdjc.auth.common.enums.AuthPermissionStatusEnum;
import com.jdjc.auth.common.enums.IsDeletedFlagEnum;
import com.jdjc.auth.domin.convert.AuthPermissionDominConverter;
import com.jdjc.auth.domin.convert.AuthRoleDominConverter;
import com.jdjc.auth.domin.entity.AuthPermissionBO;
import com.jdjc.auth.domin.redis.RedisUtil;
import com.jdjc.auth.domin.service.AuthPermissionDominService;
import com.jdjc.auth.infra.basic.entity.AuthPermission;
import com.jdjc.auth.infra.basic.entity.AuthRole;
import com.jdjc.auth.infra.basic.service.AuthPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service("AuthPermissionDominService")
@Slf4j
public class AuthPermissionDominServiceImpl implements AuthPermissionDominService {
    @Resource
    private AuthPermissionService authPermissionService;
    @Resource
    private RedisUtil redisUtil;
    // 定义权限前缀，用于构建Redis的键。
    private String authPermissionPrefix = "auth.permission";
    /**
     * 添加权限
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info(" AuthPermissionDominServiceImpl：add:BO:{}", authPermissionBO);
        }
        AuthPermission authPermission = AuthPermissionDominConverter.INSTANCE.convertBOToEntity(authPermissionBO);
        authPermission.setStatus(AuthPermissionStatusEnum.OPEN.getCode());
        authPermission.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count =authPermissionService.insert(authPermission);
        return count > 0;
    }

    /**
     * 更新权限
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info(" AuthPermissionDominServiceImpl：update:BO:{}", authPermissionBO);
        }
        AuthPermission authPermission = AuthPermissionDominConverter.INSTANCE.convertBOToEntity(authPermissionBO);
        int count =authPermissionService.update(authPermission);
        return count > 0;
    }

    /**
     * 逻辑删除权限
     * @param authPermissionBO
     * @return
     */
    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info(" AuthPermissionDominServiceImpl：delete:BO:{}", authPermissionBO);
        }
        AuthPermission authPermission = new AuthPermission();
        authPermission.setId(authPermissionBO.getId());
        authPermission.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count =authPermissionService.update(authPermission);
        return count > 0;
    }

    /**
     * 查询用户权限
     * @param userName
     * @return
     */
    @Override
    public List<String> getPermission(String userName) {
        // 构建权限键名，结合特定的前缀和用户名，例如auth.permission.鸡翅
        String permissionKey = redisUtil.buildKey(authPermissionPrefix, userName);
        // 根据权限键名从redis中获取权限
        String permissionValue = redisUtil.get(permissionKey);
        if (StringUtils.isBlank(permissionValue)){
            return Collections.emptyList();
        }
        // 将 JSON 字符串转换成 AuthPermission 对象列表
        List<AuthPermission> permissionList = new Gson().fromJson(permissionValue, new TypeToken<List<AuthPermission>>() {
        }.getType());
        // 从权限列表中提取权限键，并收集到一个新的列表中
        List<String> authList = permissionList.stream().map(AuthPermission::getPermissionKey).collect(Collectors.toList());
        return authList;
    }
}
